package com.shao.web;

import com.shao.pojo.User;
import com.shao.service.UserService;
import com.shao.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: SJP
 * Date: 2021/5/12
 * Time: 17:09
 * Description: No Description
 */
public class LoginServlet extends HttpServlet {

    UserService userService = new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //调用xxxservice.xx方法执行业务
        if((userService.login(new User(null,username,password,null))==null)){
            //把错误信息和要回显的表单项信息，保存到request域中（键值对对象）
            req.setAttribute("msg","用户名或密码错误");
            req.setAttribute("username",username);
            //无效用户名或密码跳回登录页面
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp);
        }else{
            //跳转到登录成功页面
            req.getRequestDispatcher("/pages/user/login_success.jsp").forward(req,resp);
        }

    }
}
